package com.zone.sdk.module.appstore;

import com.zone.sdk.common.http.base.BaseCloudServiceManager;
import com.zone.sdk.common.http.base.BaseRequest;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.appstore.base.AppStoreResult;
import com.zone.sdk.module.appstore.entity.AppDetailEntity;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.sdk.module.appstore.entity.AppListItemEntity;
import com.zone.sdk.module.appstore.http.AppStoreOkHttpManager;
import com.zone.sdk.module.appstore.request.AppsDetailRequest;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import io.reactivex.observers.DisposableObserver;

public class CloudAppStoreServiceManager extends BaseCloudServiceManager {

    public static CloudAppStoreServiceManager instance;

    private final ICloudAppStoreService iCloudAppStoreService;

    private CloudAppStoreServiceManager(String baseUrl) {
        super();
        iCloudAppStoreService = AppStoreOkHttpManager.getInstance(baseUrl).build(ICloudAppStoreService.class);
    }

    public static CloudAppStoreServiceManager getInstance(String baseUrl) {
        if (instance == null){
            instance = new CloudAppStoreServiceManager(baseUrl);
            return instance;
        }
        return instance;
    }


    /**
     * 显示app总体评分和所有用户评价列表
     * @param request
     * @param callback
     */
    public void homeAppList(BaseRequest request, ResultCallBack<Result<AppStoreResult<AppListItemEntity>>> callback){
        observableService.execute(iCloudAppStoreService.homeAppList(request), new DisposableObserver<Result<AppStoreResult<AppListItemEntity>>>(){

            @Override
            public void onNext(@NotNull Result<AppStoreResult<AppListItemEntity>> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
//        innerExecute(iCloudAppStoreService.homeAppList(request), callback);
    }

    /**
     * 应用详情
     * @param request
     * @param callback
     */
    public void appsDetail(AppsDetailRequest request, ResultCallBack<Result<AppDetailEntity>> callback){
        observableService.execute(iCloudAppStoreService.appsDetail(request) , new DisposableObserver<Result<AppDetailEntity>>(){

            @Override
            public void onNext(@NotNull Result<AppDetailEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 应用详情
     * @param request
     * @param callback
     */
    public void appList(AppsDetailRequest request, ResultCallBack<Result<List<AppEntity>>> callback){
        observableService.execute(iCloudAppStoreService.appList(request) , new DisposableObserver<Result<List<AppEntity>>>(){

            @Override
            public void onNext(@NotNull Result<List<AppEntity>> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }
}


